﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SPFoundation
{
    public class ULSLogging : SPDiagnosticsServiceBase
    {
        public static string DiagnosticAreaName = "Custom";
        private static ULSLogging _Current = null;
        public const string INTERACTION_INTEGRATION = "InterActionIntegration";

        private static readonly object padlock = new object();

        public static ULSLogging Current
        {
            get
            {
                lock (padlock)
                {
                    if (_Current == null)
                    {
                        _Current = new ULSLogging();
                    }
                    return _Current;
                }
            }
        }

        private ULSLogging() : base("Custom Logging Service", SPFarm.Local) { }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea> 
            { 
                new SPDiagnosticsArea(DiagnosticAreaName, new List<SPDiagnosticsCategory> { 
                    new SPDiagnosticsCategory("WebParts", TraceSeverity.Unexpected, EventSeverity.Error),
                    new SPDiagnosticsCategory(INTERACTION_INTEGRATION, TraceSeverity.Unexpected, EventSeverity.Error)
                }) 


            };
            return areas;
        }

        public static void LogError(string categoryName, string errorMessage)
        {
            SPDiagnosticsCategory category = ULSLogging.Current.Areas[DiagnosticAreaName].Categories[categoryName];
            ULSLogging.Current.WriteTrace(0, category, TraceSeverity.Unexpected, errorMessage);
        }

        public static void LogDebug(string categoryName, string errorMessage)
        {
            SPDiagnosticsCategory category = ULSLogging.Current.Areas[DiagnosticAreaName].Categories[categoryName];
            ULSLogging.Current.WriteTrace(0, category, TraceSeverity.Verbose, errorMessage);
        }
    }
}
